package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.dao.impl.EditAminDaoImpl;
import com.javasm.entity.AdminMenu;
import com.javasm.entity.AdminUser;
import com.javasm.entity.ReturnCode;
import com.javasm.service.impl.EditMenuServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author WangJie
 * @version 1.0
 * @Description: 菜单的增删改
 * @date: 2020/7/19 20:16
 * @since JDK 1.8
 */

@WebServlet("/editMenu")
public class EditMenuServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        Map<String, Object> returnData = new HashMap<>();
        int result = 0;
        if ("insertMenuInfo".equals(method)){
            result = insertMenuInfo(req,resp);
        } else if ("updateMenuInfo".equals(method)){
            result = updateMenuInfo(req,resp);
        } else if("deleteMenuInfo".equals(method)){
            result = deleteMenuInfo(req,resp);
        }
        // 操作是否成功的判断
        if (result >= 1) {
            returnData.put("returnCode", ReturnCode.SUCCESS.getCode());
            returnData.put("returnMsg", ReturnCode.SUCCESS.getMsg());
        } else {
            returnData.put("returnCode", ReturnCode.ERROR.getCode());
            returnData.put("returnData", ReturnCode.ERROR.getMsg());
        }
        PrintWriter pw = resp.getWriter();
        pw.print(JSON.toJSONString(returnData));
        pw.flush();
        pw.close();
    }

    // 通过id删除菜单信息
    private int deleteMenuInfo(HttpServletRequest req, HttpServletResponse resp) {
        String pid = req.getParameter("pid");
        return new EditMenuServiceImpl().deleteMenuInfo(pid);
    }

    // 修改菜单信息
    private int updateMenuInfo(HttpServletRequest req, HttpServletResponse resp) {
        AdminMenu adminMenu = new AdminMenu();
        adminMenu.setPid(Integer.parseInt(req.getParameter("pid")));
        adminMenu.setPname(req.getParameter("pname"));
        adminMenu.setFid(Integer.parseInt(req.getParameter("fid")));
        adminMenu.setIsshow(Integer.parseInt(req.getParameter("isshow")));
        adminMenu.setUrl(req.getParameter("url"));
        adminMenu.setGlyphicon(req.getParameter("glyphicon"));
        return new EditMenuServiceImpl().updateMenuInfo(adminMenu);
    }

    // 新增菜单
    private int insertMenuInfo(HttpServletRequest req, HttpServletResponse resp) {
        AdminMenu adminMenu = new AdminMenu();
        adminMenu.setPid(Integer.parseInt(req.getParameter("pid")));
        adminMenu.setPname(req.getParameter("pname"));
        adminMenu.setFid(Integer.parseInt(req.getParameter("fid")));
        adminMenu.setIsshow(Integer.parseInt(req.getParameter("isshow")));
        adminMenu.setUrl(req.getParameter("url"));
        adminMenu.setGlyphicon(req.getParameter("glyphicon"));
        return new EditMenuServiceImpl().insertMenuInfo(adminMenu);
    }
}
